<?php
namespace api\modules\v1\controllers;
use Yii;
use yii\base\Exception;
use yii\web\HttpException;


class PublishpcController extends PublishController
{
    public $modelClass = 'api\modules\v1\models\Publishpc';

    protected $_is_sign = false; //是否进行机器码Sign签名验证

    public function behaviors() {
        if(in_array(Yii::$app->controller->action->id,['news','yulan','edit','del'])){
            $this->_is_auth = true;
        }else{
            $this->_is_auth = false;
        }
        return parent::behaviors();
    }

    /*
     * 文章列表
     */
    public function prepareDataProvider()
    {
        $params = \Yii::$app->request->queryParams;
        if(!isset($params['type']) || !isset($params['uid'])){
            throw new HttpException(401,'参数不全',0);
        }
        $modelClass = $this->modelClass;
        $searchModel = new $modelClass();
        $searchModel->scenario='search';
        $dataProvider = $searchModel->search($params);
        return $dataProvider;
    }
    /*
     * 文章发布-网页版
     * @return mixed
     * @throws HttpException
     * @throws ServerErrorHttpException
     */
    public function actionNews(){
        $this->layout = false;
        $model = new $this->modelClass();
        $model->scenario='publish';
        if (Yii::$app->request->post()) {
            $model->attributes = Yii::$app->request->post();
            $model->pubdate=date('Y-m-d H:i:s',time());
            $model->author=Yii::$app->user->id;
            $model->is_delete=0;
            $message = ($model->status == 1)?'文章发布成功!':'保存草稿成功!';
            if($model->news()){
                return [
                    "message"=>$message,
                    "id" => $model->id,
                ];
            }elseif (!$model->hasErrors()) {
                throw new ServerErrorHttpException('由于不明原因未能创建对象.');

            }else{
                throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
            }
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }
    /*
     * 文章修改-网页版
     * @return mixed
     * @throws HttpException
     * @throws ServerErrorHttpException
     */
    public function actionEdit($id){
        $this->layout = false;
        $id = (int)$id;
        $model = $this->findModel($id);
        $model->scenario='edit';
        $model->pubdate = date("Y-m-d H:i:s");
        if (Yii::$app->request->post()) {
            $model->attributes = Yii::$app->request->post();
            $message = ($model->status == 1)?'文章发布成功!':'保存草稿成功!';
            if($model->edit()){
                return [
                    "message"=> $message,
                    "id" => $model->id,
                ];
            }elseif (!$model->hasErrors()) {
                throw new ServerErrorHttpException('由于不明原因未能创建对象.');
            }else{
                throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
            }
        } else {
            $catname = '';
            if($model->catid){
                $modelClass = 'api\modules\v1\models\Category';
                $catModel = $modelClass::findOne($model->catid);
                if(!empty($catModel)){
                    $catname = $catModel->catname;
                }else{
                    $model->catid = '';
                }
            }
            return $this->render('create', [
                'model' => $model,
                'edit' => 1,
                'id' => $id,
                'catname' => $catname,
            ]);
        }
    }
    /*
     * 文章预览-网页版
     * @return mixed
     * @throws HttpException
     * @throws ServerErrorHttpException
     */
    public function actionYulan(){
        $this->layout = false;
        $post = Yii::$app->request->post();
        if(isset($post['title']) && $post['title'] && isset($post['content']) && $post['content']){
            return $this->render('yulan', [
                'post' => $post,
            ]);
        }else{
            throw new HttpException(401,'参数不全',0);
        }
    }
    /*
     * 文章删除-网页版
     * @return mixed
     * @throws HttpException
     * @throws ServerErrorHttpException
     */
    public function actionDel($id){
        $model = new $this->modelClass();
        if($model->del($id)){
            return [
                "message"=>'文章删除成功!',
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }
}